package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.enums.CategoryDictEnum;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyResearchActivitiesQuery;
import com.zl.tby.model.TbyCategoryDict;
import com.zl.tby.model.TbyResearchActivities;
import com.zl.tby.repository.TbyResearchActivitiesRepo;
import com.zl.tby.service.ITbyCategoryDictService;
import com.zl.tby.service.ITbyResearchActivitiesService;
import com.zl.tby.vo.TbyResearchActivitiesForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.DbContext;
import org.noear.wood.DbTableQuery;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.impl.IPageImpl;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 科研活动调查管理
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyResearchActivitiesServiceImpl implements ITbyResearchActivitiesService {

    @Db
    private TbyResearchActivitiesRepo tbyResearchActivitiesRepo;
    @Inject
    private ITbyCategoryDictService tbyCategoryDictService;
    @Db
    private DbContext db;

    @Override
    @Tran
    public R<TbyResearchActivities> add(TbyResearchActivitiesForm form) {
        TbyResearchActivities entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyResearchActivitiesRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyResearchActivities> update(TbyResearchActivitiesForm form) {
        TbyResearchActivities entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyResearchActivitiesRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public int delete(Long id) {
        return tbyResearchActivitiesRepo.deleteById(id);
    }

    @Override
    public TbyResearchActivities get(Long id) {
        return tbyResearchActivitiesRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyResearchActivitiesForm> page(TbyResearchActivitiesQuery query) {
        IPage<TbyResearchActivities> iPage = tbyResearchActivitiesRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getName()), "name like ?", "%" + query.getName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getHoldDateBegin()), "hold_date >= ?", query.getHoldDateBegin() + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(query.getHoldDateEnd()), "hold_date <= ?", query.getHoldDateEnd() + " 23:59:59");
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        Map<Long, TbyCategoryDict> dictMap = this.tbyCategoryDictService.getIdMapByCategory(CategoryDictEnum.activity_category.toString());
        List<TbyResearchActivitiesForm> formList = new ArrayList<>();
        iPage.getList().forEach(item -> {
            formList.add(TbyResearchActivitiesForm.recordConvert(item, dictMap));
        });
        return new TbyPageResult(new IPageImpl(formList, iPage.getTotal(), iPage.getSize()), query);
    }

    @Override
    public List<TbyResearchActivities> list(TbyResearchActivitiesQuery query) {
        DbTableQuery dbTableQuery = db.table("tby_research_activities ra");
        if (StringUtils.isNotEmpty(query.getCategoryCode())) {
            dbTableQuery.innerJoin("tby_category_dict cate").onEq("cate.id", "ra.category_id");
        }
        if (StringUtils.isNotEmpty(query.getTypeCode())) {
            dbTableQuery.innerJoin("tby_category_dict cateType").onEq("cateType.id", "ra.type_id");
        }
        dbTableQuery.whereTrue().andIf(StringUtils.isNotEmpty(query.getCategoryCode()), "cate.code = ?", query.getCategoryCode())
                .andIf(StringUtils.isNotEmpty(query.getTypeCode()), "cateType.code = ?", query.getTypeCode())
                .andIf(StringUtils.isNotEmpty(query.getHoldDateBegin()), "hold_date >= ?", query.getHoldDateBegin() + " 00:00:00")
                .andIf(StringUtils.isNotEmpty(query.getHoldDateEnd()), "hold_date <= ?", query.getHoldDateEnd() + " 23:59:59");
        try {
            return dbTableQuery.selectList("ra.*", TbyResearchActivities.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ArrayList<>();
    }
}
